import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
// 导入组件
const routes = [
  // 登录注册
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/register.vue'),
  },
  // 跳转测试页
  {
    path: '/home',
    name: 'Home',
    component: () => import('../views/home.vue'),
  },
  // 教师端路由
  {
    path: '/teacher/dashboard',
    name: 'TeacherDashboard',
    component: () => import('../views/teacher/dashboard/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'teacher',
      title: '教师仪表盘'
    }
  },
  {
    path: '/ai',
    name: 'ai_conversion',
    component: () => import('../views/ai/ai_conversion.vue'),
    meta: {
      requiresAuth: true,
      title: 'AI助手'
    }
  },
  {
    path: '/teacher/assignment/assign_homework',
    name: 'assign_homework',
    component: () => import('../views/teacher/assignment/assign_homework.vue'),
    meta: {
      requiresAuth: true,
      title: '布置作业'
    }
  },
  {
    path: '/teacher/assignment/see_homework',
    name: 'see_homework',
    component: () => import('../views/teacher/assignment/see_homework.vue'),
    meta: {
      requiresAuth: true,
      title: '查看作业'
    }
  },
  {
    path: '/teacher/assignment/correct_homework',
    name:'correct_homework',
    component: () => import('../views/teacher/assignment/correct_homework.vue'),
    meta: {
      requiresAuth: true,
      title: '批改作业'
    }
  },
  {
    path: '/teacher/assignment/:id',
    name: 'TeacherAssignment',
    component: () => import('../views/teacher/assignment/card/index.vue'),
    meta: {
      requiresAuth: true,
      title: '作业详情'
    }
  },
  {
    path: '/teacher/feedback/:id',
    name: 'TeacherFeedback',
    component: () => import('../views/teacher/feedback/card/index.vue'),
    meta: {
      requiresAuth: true,
      title: '质量反馈'
    }
  },
  {
    path: '/teacher/feedback/result/index',
    name: 'feedbackresult',
    component: () => import('../views/teacher/feedback/result/index.vue'),
    meta: {
      requiresAuth: true,
      title: '反馈结果'
    }
  },
  {
    path: '/teacher/signin/signin',
    name: 'signin',
    component: () => import('../views/teacher/signin/signin.vue'),
    meta: {
      requiresAuth: true,
      title: '签到'
    }
  },
  {
    path: '/teacher/chat/index',
    name:'AIGCconversion',
    component: () => import('../views/teacher/chat/index.vue'),
    meta: {
      requiresAuth: true,
      title: 'AIGC智能聊天室'
    }
  },
  {
    path: '/teacher/chat',
    name: 'TeacherChat',
    component: () => import('../views/teacher/chat/index.vue'),
    meta: {
      requiresAuth: true,
      title: '智能聊天'
    }
  },
  {
    path: '/teacher/record/:id',
    name: 'TeacherRecord',
    component: () => import('../views/teacher/record/card/index.vue'),
    meta: {
      requiresAuth: true,
      title: '课堂总结'
    }
  },
  {
    path: '/teacher/feedback/:id',
    name: 'TeacherFeedback',
    component: () => import('../views/teacher/feedback/card/index.vue'),
    meta: {
      requiresAuth: true,
      title: '质量反馈卡片'
    }
  },
  {
    path: '/teacher/assignment/card',
    name: 'TeacherAssignmentCard',
    component: () => import('../views/teacher/assignment/card/index.vue'),
    meta: {
      requiresAuth: true,
      title: '教室作业卡片'
    }
  },
  {
    path: '/teacher/manager',
    name: 'TeacherManager',
    component: () => import('../views/teacher/manager/index.vue'),
    meta: {
      requiresAuth: true,
      title: '教师管理'
    } 
  },
  {
    path: '/teacher/manager/course/details/:courseManageId',
    name: 'CourseDetails',
    component: () => import('@/views/teacher/manager/course/details.vue'),
    props: true,
    meta: {
      requiresAuth: true,
      title: '课程详情'
    }
  },
  {
    path: '/teacher/manager/lessonschedule/:courseManageId',
    name: 'TeacherManagerCourseLessonSchedule',
    component: () => import('../views/teacher/manager/course/lessonschedule.vue'),
    meta: {
      requiresAuth: true,
      title: '课程课时安排'
    }
  },
  {
    path: '/teacher/manager/course/edit/:id',
    name: 'TeacherManagerCourseEdit',
    component: () => import('../views/teacher/manager/course/editcourse.vue'),
    meta: {
      requiresAuth: true,
      title: '编辑课程'
    } 
  },
  {
    // path: '/teacher/manager/student/:courseManageId',
    path: '/teacher/manager/student',
    name: 'TeacherManagerStudent',
    component: () => import('../views/teacher/manager/course/studentManager.vue'),
    meta: {
      requiresAuth: true,
      title: '学生管理'
    } 
  },
  {
    // path: '/teacher/manager/interaction/:id',
    path: '/teacher/manager/interaction',
    name: 'TeacherManagerInteraction',
    component: () => import('../views/teacher/manager/course/interaction.vue'),
    meta: {
      requiresAuth: true,
      title: '互动管理'
    } 
  },
  {
    // path: '/teacher/manager/dialogue/:id',
    path: '/teacher/manager/dialogue',
    name: 'TeacherManagerDialogue',
    component: () => import('../views/teacher/manager/course/dialogue.vue'),
    meta: {
      requiresAuth: true,
      title: '对话管理'
    } 
  },
  {
    path: '/teacher/manager/class/:id',
    name: 'TeacherManagerClass',
    component: () => import('../views/teacher/manager/course/class.vue'),
    meta: {
      requiresAuth: true,
      title: '课时管理'
    } 
  },
  {
    path: '/teacher/manager/addcourse',
    name: 'TeacherAddCourse',
    component:()=>import('../views/teacher/manager/course/addcourse.vue'),
    meta:{
      requiresAuth:true,
      role:'teacher',
      title:'添加课程'
    }
  },
  //学生端路由
  {
    path: '/student/dashboard',
    name: 'StudentDashboard',
    component: () => import('../views/student/dashboard/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '学生仪表盘'
    }
  },
  {
    path: '/student/homework/card',
    name: 'StudentHomeworkCard',
    component: () => import('../views/student/homework/card/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '作业卡片'
    }
  },
  {
    path: '/student/homework/submit/submit',
    name: 'submit',
    component: () => import('../views/student/homework/submit/submit.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '提交作业'
    }
  },
  {
    path: '/student/feedback/card',
    name: 'StudentFeedbackCard',
    component: () => import('../views/student/feedback/card/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '课堂质量实时反馈'
    }
  },
  {
    path: '/student/mycourse',
    name: 'StudentMycourse',
    component: () => import('../views/student/mycourse/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '我的课程'
    }
  },
  {
    path: '/student/mycourse/details/:id',
    name: 'StudentMycourseDetails',
    component: () => import('../views/student/mycourse/details.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '课程详情'
    }
  },
  {
    path: '/student/homework/see_correcthomework',
    name: 'see_correcthomework',
    component: () => import('../views/student/homework/see_correcthomework.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '查看批改情况'
    }
  },
  {
    path: '/student/signin/index',
    name: 'student_signin',
    component: () => import('../views/student/signin/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '签到'
    }
  },
  {
    path: '/student/suggestion/index',
    name: 'student_suggestion',
    component: () => import('../views/student/suggestion/index.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '意见反馈问卷'
    }
  },
  {
    path: '/student/chat',
    name: 'student_chat',
    component: () => import('../views/student/chat.vue'),
    meta: {
      requiresAuth: true,
      role: 'student',
      title: '聊天室'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: () => import('@/views/login.vue'),
      meta: { requiresAuth: false }
    },
    {
      path: '/teacher/course/student-manager',
      name: 'StudentManager',
      component: () => import('@/views/teacher/manager/course/studentManager.vue'),
      meta: { requiresAuth: true }
    },
    ...routes
  ]
})

// 导航守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  
  if (to.meta.requiresAuth && !token) {
    ElMessage.warning('请先登录');
    next('/login');
  } else {
    next();
  }
});

export default router;
